package com.cmhi.base.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.softmbh.kellyplatform.util.AppUtils;


public class BimProvider extends ContentProvider {


    public static final String AUTHORITY = "stbconfig"; // 与AndroidManifest保持一致
    public static final Uri HUA_WEI_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BimDBHelper.HUA_WEI_TABLE_NAME);
    public static final Uri SHARE_INFO_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BimDBHelper.SHARE_INFO_TABLE_NAME);

    public static final int HUA_WEI_URI_CODE = 0;
    public static final int SHARE_INFO_URI_CODE = 1;

    private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    // 关联Uri和Uri_Code
    static {
        sUriMatcher.addURI(AUTHORITY, BimDBHelper.HUA_WEI_TABLE_NAME, HUA_WEI_URI_CODE);
        sUriMatcher.addURI(AUTHORITY, BimDBHelper.SHARE_INFO_TABLE_NAME, SHARE_INFO_URI_CODE);
    }

    private SQLiteDatabase mDb;

    @Override
    public boolean onCreate() {
        AppUtils.debugLog("onCreate");
        mDb = new BimDBHelper(getContext()).getWritableDatabase();
        return true;
    }

    private String getTableName(Uri uri) {
        String tableName = null;
        switch (sUriMatcher.match(uri)) {
            case HUA_WEI_URI_CODE:
                tableName = BimDBHelper.HUA_WEI_TABLE_NAME;
                break;
            case SHARE_INFO_URI_CODE:
                tableName = BimDBHelper.SHARE_INFO_TABLE_NAME;
                break;
            default:
                break;
        }
        return tableName;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri,
                        @Nullable String[] projection,
                        @Nullable String selection,
                        @Nullable String[] selectionArgs,
                        @Nullable String sortOrder) {
        String tableName = getTableName(uri);
        if (TextUtils.isEmpty(tableName)) {
            throw new IllegalArgumentException("Unsupported URI: " + uri);
        }
        return mDb.query(tableName, projection, selection, selectionArgs, null, null, sortOrder, null);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        String table = getTableName(uri);
        if (TextUtils.isEmpty(table)) {
            throw new IllegalArgumentException("Unsupported URI: " + uri);
        }
        mDb.insert(table, null, values);
        // 插入数据后通知改变
        getContext().getContentResolver().notifyChange(uri, null);
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri,
                      @Nullable String selection,
                      @Nullable String[] selectionArgs) {

        String table = getTableName(uri);
        if (TextUtils.isEmpty(table)) {
            throw new IllegalArgumentException("Unsupported URI: " + uri);
        }
        int count = mDb.delete(table, selection, selectionArgs);
        if (count > 0) {
            getContext().getContentResolver().notifyChange(uri, null);
        }
        return count;
    }

    @Override
    public int update(@NonNull Uri uri,
                      @Nullable ContentValues values,
                      @Nullable String selection,
                      @Nullable String[] selectionArgs) {
        String table = getTableName(uri);
        if (TextUtils.isEmpty(table)) {
            throw new IllegalArgumentException("Unsupported URI: " + uri);
        }
        int row = mDb.update(table, values, selection, selectionArgs);
        if (row > 0) {
            getContext().getContentResolver().notifyChange(uri, null);
        }
        return row; // 返回更新的行数
    }
}